package org.example.dxgi;

import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import org.apache.commons.io.IOUtils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.image.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Base64;

public class Test {
    private static CDXGILibrary dxgi = CDXGILibrary.INSTANCE;

    public static void main(String[] args) throws IOException {
        System.loadLibrary("opencv_java455");

        Rectangle rect = new Rectangle(600, 400, 83, 123); // 你可以根据需要设置屏幕尺寸
//        rect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        int dataSize = (rect.width * rect.height * 4);
        Pointer imagePointer = new Memory(dataSize);
        dxgi.init();
        dxgi.getImg(imagePointer);
        byte[] pixels = imagePointer.getByteArray(0, dataSize);
        BufferedImage rgbImage = createRGBImage(pixels, rect.width, rect.height);
//        byte[] compData = data;
//        byte[] res = imgPixelByteToPng(compData, rect);

//        BufferedImage bufferedImage = imgPixelByteToPng(pixels, rect);
        ImageIO.write(rgbImage, "png", new File("test1.png"));
    }

    private static byte[] compare(byte data1[], byte data2[]) {
        byte[] data = new byte[data1.length];
        for (int i = 0; i < data.length; i++) {
            if (data2[i] ==  127)
                data[i] =  126;
            else if (data1[i] == data2[i])
                data[i] =  127;
            else
                data[i] = data2[i];
        }
        return data;
    }

    private static BufferedImage imgPixelByteToPng(byte[] data, Rectangle rect) {
        Mat screen = new Mat(rect.height, rect.width, CvType.CV_8UC4);
        screen.put(0, 0, data);
        Imgproc.cvtColor(screen, screen, Imgproc.COLOR_BGRA2RGB);
        Imgcodecs.imwrite("example_write.jpg", screen);
//        String s = Base64.getEncoder().encodeToString(data);
        // 转换为 RGB
        Mat rgb = screen;//new Mat();
//        Imgproc.cvtColor(screen, rgb, Imgproc.COLOR_BGRA2RGB);
        // 创建 BufferedImage
        BufferedImage image = new BufferedImage(rgb.cols(), rgb.rows(), BufferedImage.TYPE_3BYTE_BGR);
        return image;
    }

    public static BufferedImage createRGBImage(byte[] data, int width, int height){
        int bytePerPixel = 4;
        if (null !=data && data.length == width * height * bytePerPixel){
            return null;
        }
        DataBufferByte dataBuffer = null == data ? null : new DataBufferByte(data, data.length);
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        int[] bOffs = {2,1,0};
        ComponentColorModel colortodel = new ComponentColorModel(cs, false, false, Transparency.OPAQUE,DataBufferByte.TYPE_BYTE);
        WritableRaster raster = null != dataBuffer ? Raster.createInterleavedRaster(dataBuffer,width,height,width*bytePerPixel,bytePerPixel,bOffs, null)
                : Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, width*bytePerPixel,bytePerPixel, bOffs, null);
        BufferedImage img = new BufferedImage(colortodel, raster, colortodel.isAlphaPremultiplied(), null);
        return img;
    }
}
